﻿ko.bindingHandlers.datepicker = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datepickerOptions || {};

        if (options.buttonImage == null && options.showOn == null) {
            options.buttonImage = "/Images/icon-calendar.png";
            options.buttonImageOnly = true;
            options.showOn = "button";
        }
        options.dateFormat = "dd.mm.yy";

        $(element).datepicker(options);
        //$(element).mask("99.99.9999");

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            observable($(element).datepicker("getDate"));
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).datepicker("destroy");
        });

    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        value = new Date(value);
        //handle date data coming via json from Microsoft
       /* if (String(value).indexOf('/Date(') == 0) {
            value = new Date(parseInt(value.replace(/\/Date\((.*?)\)\//gi, "$1")));
        }*/
        current = $(element).datepicker("getDate");

        if (value - current !== 0) {
            $(element).datepicker("setDate", value);
        }
    }
};

//todo check data +3
ko.bindingHandlers.dateshower= {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        var d = new Date(value);
        var date = d.getDate();
        var month = d.getMonth() + 1; //Months are zero based
        var year = d.getFullYear();
        
        $(element).text(date + "-" + month + "-" + year);

    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        var d = new Date(value);
        var date = d.getDate();
        var month = d.getMonth() + 1; //Months are zero based
        var year = d.getFullYear();

        $(element).text(date + "-" + month + "-" + year);
        
        
    }
}

ko.bindingHandlers.koautocomplete = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var func = allBindingsAccessor().func || {};
        
        
        var serviceUrl = "/Medora.Web" + allBindingsAccessor().serviceUrl || "/api/Communication/GetAutocomplete";
        var minChars = allBindingsAccessor().minChars || 1;
        
        ko.utils.registerEventHandler(element, 'change', function (event) {

            var curPage = $(element).val();
            var value = valueAccessor();
            value(curPage);
        });
        
        var options = {
            serviceUrl: serviceUrl,
            minChars: minChars,
            //params: {descriptor:"{Model:'Form', Field: 'Country'}"},
            onSelect: func
        };
        $(element).autocomplete(options);
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value != null) $(element).val(value);

    }
};

ko.extenders.required = function (target, overrideMessage) {

    //add some sub-observables to our observable
    target.hasError = ko.observable();
    target.validationMessage = ko.observable();

    //define a function to do validation
    function validate(newValue) {
        target.hasError(newValue ? false : true);
        target.validationMessage(newValue ? "" : overrideMessage || "This field is required");
    }

    //initial validation
    validate(target());

    //validate whenever the value changes
    target.subscribe(validate);

    //return the original observable
    return target;
};

ko.extenders.validationEvent = function (target, option) {
    target.hasError = ko.observable();
    target.validationMessage = ko.observable();
   /* target.subscribe(function (newValue) {
        console.log(option + " has error: " + target.hasError());
    });*/
    return target;
};